package com.bioscript.standalone.imps;

import java.util.List;

import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import com.bioscript.standalone.IO.FileWriter;
import com.bioscript.standalone.dataStructures.Generation;
import com.bioscript.standalone.dataStructures.Project;

public class ProjectDescription {
	
	List<Project> lst = null;
	
	public ProjectDescription parse(String fileName)
	{
		try
		{
			Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(fileName);
			NodeList list = doc.getElementsByTagName("prj");
			lst = Project.getProjects(list);
			return this;
		}
		catch(Exception e)
		{
			e.printStackTrace();
			return null;
		}
	}
	
	public List<Project> getLst() {
		return lst;
	}

	public void addGeneration(String projName, String gen, String type)
	{
		for (Project project : lst)
		{
			if (project.getName().equals(projName))
			{
				Generation generation = new Generation(gen, type);
				project.addGeneration(generation);
				break;
			}
		}
	}
	
	public boolean writeProjectDescription(String fileName, Project prj)
	{
		try
		{
			Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
			if (prj.toXML(document) && FileWriter.writeXML(document, fileName))
			{
				return true;
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return false;
	}
	
	public String getProjectString()
	{
		try
		{
			Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
			Project prj = lst.get(0);
			if (!prj.toXML(document)) return null;
			return FileWriter.makeXMLString(document);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return null;
	}
}
